package com.garlic.face.jlf.presenter;

import com.garlic.face.jlf.model.AuthBean;
import com.garlic.face.jlf.model.OrderBean;
import com.garlic.face.jlf.model.TradeBean;

/**
 * @author wenhui02.liu
 * @date 2018/8/13.
 */

public class PaySuccessPresenter implements PaySuccessContract.Presenter {

    String TAG = "PaySuccessPresenter";
    PaySuccessContract.View mView;

    public PaySuccessPresenter(PaySuccessContract.View view) {
        mView = view;
    }

    @Override
    public void doneOrder(boolean exit, AuthBean authBean) {
        //找到所有没有提交的订单
            /*try {
                List<SqlOrderBean> tmpList = App.getDb().selector(SqlOrderBean.class).where
                        ("store_code", "=", authBean.getStore_code()).and
                        ("order_back", "=", "N").findAll();
                Log.e(TAG, JSON.toJSONString(tmpList));
                for (final SqlOrderBean bean : tmpList) {
                    RequestParams params = new RequestParams(App.getFaceSrvPrefixUrl() + "order/done");
                    params.addParameter("store_code", bean.getStore_code());
                    params.addParameter("device_num", bean.getDevice_num());
                    params.addParameter("down_trade_no", bean.getDown_trade_no());
                    params.addParameter("pay_scene", bean.getPay_scene());
                    params.addParameter("pay_channel", bean.getPay_channel());
                    params.addParameter("request_trade_no", bean.getRequest_trade_no());
                    params.addParameter("channel_trade_no", bean.getChannel_trade_no());
                    params.addParameter("buyer_user_id", bean.getBuyer_user_id());
                    params.addParameter("buyer_amount", bean.getBuyer_amount());
                    params.addParameter("seller_coupon", bean.getSeller_coupon());
                    params.addParameter("chn_coupon", bean.getChn_coupon());
                    params.addParameter("version_code", bean.getVersion_code());
                    x.http().post(params, new Callback.CommonCallback<String>() {
                        @Override
                        public void onSuccess(String result) {
                            Log.e(TAG, result);
                            BaseBean baseBean = new BaseBean();
                            baseBean.parse(result);
                            if (baseBean.isOk()) {
                                //请求成功了..则保存这个单子...
                                bean.setOrder_back("Y");
                                Log.e(TAG, JSON.toJSONString(bean));
                                try {
                                    App.getDb().update(bean);
                                } catch (DbException e) {
                                    e.printStackTrace();
                                    if (!TextUtils.isEmpty(e.getMessage())) {
                                        Log.e(TAG, e.getMessage());
                                    }
                                }
                            }
                        }

                        @Override
                        public void onError(Throwable ex, boolean isOnCallback) {
                        }

                        @Override
                        public void onCancelled(CancelledException cex) {
                        }

                        @Override
                        public void onFinished() {
                        }
                    });
                }
                //搞其他事情
                if (exit) {
                    try {
                        mView.destoryTimer();
                        App.clearToMainActivity();
                    } catch (Exception e) {
                        e.printStackTrace();
                        if (!TextUtils.isEmpty(e.getMessage())) {
                            Log.e(TAG, e.getMessage());
                        }
                    }
                }
            } catch (DbException e) {
                if (!TextUtils.isEmpty(e.getMessage())) {
                    e.printStackTrace();
                    Log.e(TAG, e.getMessage());
                }
            }*/
    }

    @Override
    public void saveOrderLocal(AuthBean mAuthBean, OrderBean mOrderBean, String mScene, TradeBean mTradeBean) {
        //把数据保存到本地数据库
        /*SqlOrderBean sqlOrderBean = new SqlOrderBean();
        sqlOrderBean.setStore_code(mAuthBean.getStore_code());
        sqlOrderBean.setDevice_num(mAuthBean.getDevice_num());
        sqlOrderBean.setDown_trade_no(mOrderBean.getDown_trade_no());
        sqlOrderBean.setPay_scene(mScene);
        if (mTradeBean.getTrade_channel().equals("ali")) {
            sqlOrderBean.setPay_channel("ALIPAY");
        } else if (mTradeBean.getTrade_channel().equals("wx")) {
            sqlOrderBean.setPay_channel("WECHAT");
        } else {
            sqlOrderBean.setPay_channel(mTradeBean.getTrade_channel());
        }
        sqlOrderBean.setRequest_trade_no(mTradeBean.getTrade_no());
        sqlOrderBean.setChannel_trade_no(mTradeBean.getUp_trade_no());
        sqlOrderBean.setBuyer_user_id(mTradeBean.getBuyer_id());
        sqlOrderBean.setBuyer_amount(mTradeBean.getBuyer_amount());
        sqlOrderBean.setSeller_coupon(mTradeBean.getSeller_coupon());
        sqlOrderBean.setChn_coupon(mTradeBean.getChn_coupon());
        sqlOrderBean.setVersion_code(Tools.getVersionCode() + "");
        sqlOrderBean.setOrder_back("N");

        LogUtil.d(" "+sqlOrderBean);
        try {
            App.getDb().saveOrUpdate(sqlOrderBean);
            LogUtil.d("save db success");
        } catch (DbException e) {
            e.printStackTrace();
            if (!TextUtils.isEmpty(e.getMessage())) {
                Log.e(TAG, e.getMessage());
            }
        }*/
    }
}
